﻿<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>云笔记</title>
    <link rel="stylesheet" href="${ctxStatic}/note/css/main.css" type="text/css">
</head>

<body>
<div class="starter">
    <ul>
	   <li><input type="button" onclick="logout()" value="登出" /></li>
        <li id="user_name">您好，XXX</li>
    </ul>
</div>
<div class="header"><img src="${ctxStatic}/note/imges/cloud.png" alt="云图标"> 云笔记</div>
<div class="main">
    <img src="${ctxStatic}/note/imges/timg.jpg" alt="背景图片" id="back_img">
    <div class="main_left">
        <!--菜单部分-->
        <form action="" method="get" name="menulist">
            <table class="menu_buttons">
                <tr>
                    <td class="search" ><input type="text" id="search_title" placeholder="搜索笔记"></td>
                    <td class="search" ><input type="button" onclick="search_button()" value="搜索"></td>
                </tr>
                <tr>
                    <td class="menu" id="new_note" colspan="2"><input type="button" class="left_button" value="新建笔记" onclick="window.location.href='/i/noteuser/noteUser/tonewnote'" ></td>
                </tr>
                <tr>
                    <td class="menu" id="my_notes" colspan="2"><input type="button" class="left_button" value="我的文件夹" onclick="window.location.href='/i/noteuser/noteUser/tomynotes'"></td>
                </tr>
                <tr>
                    <td class="menu" id="my_recycle" colspan="2"><input type="button" class="left_button" value="回收站" onclick="window.location.href='/i/noteuser/noteUser/torecover'"></td>
                </tr>
            </table>
        </form>
    </div>
    <div class="main_right">
        <input class="control_list" id="edit"  type="button" value="编辑">
        <div class="control_list"></div>
        <article>
            <h3 id="note_title">笔记标题</h3>
            <p>创建日期：<span id="date">2017-07-17</span></p>
            <div class="depart"></div>
            <div id="article">
                笔记内容
            </div>
        </article>
    </div>
</div>
<div class="footer"><p>@wedo团队</p><p>联系我们 7654321</p></div>

<script > 
//EL获取userId
var userId="<%=session.getAttribute("userId")%>";
/**
 * Created by root on 2017/8/1.
 * 返回主页面的最新笔记
 */
window.onload=function(){
	//EL获取username
	var username="<%=session.getAttribute("userName")%>";

	document.getElementById("user_name").innerHTML="您好，"+username;
    var mytitle=document.getElementsByTagName("article");
    var mydate=document.getElementById("date");
    var mytext=document.getElementsByTagName("article");
    
    var new_note_button		=document.getElementById("new_note");
	var list_show_button	=document.getElementById("my_notes");
	var recycle_show_button	=document.getElementById("my_recycle");
  //  var note_id=document.getElementById("note_id");
   // var new_button=document.getElementById("left_button");
	var edit_button=document.getElementById("edit");
	
//加载最新笔记
    var request=new XMLHttpRequest();
    request.open("POST","/i/note/note/getLast",true);
	
	var sendData="userId="+userId.toString();
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.onreadystatechange=function(){
        if(request.readyState==4)
        {
            if(request.status==200)
            {
                var data=JSON.parse(request.responseText);
                //检测
                if(data.result==1)
                {              				          
					document.getElementById("note_title").innerHTML=data.title;
					document.getElementById("date").innerHTML=data.updateTime;
					document.getElementById("article").innerHTML=data.text;
					//设置cookie
					setCookie("noteId",data.noteId,1);
					setCookie("userId",userId,1);
					setCookie("userName",username,1)
                }
            }
            else
            {
                alert("请求错误"+request.status);
            }
        }
    }
    request.send(sendData);


	//编辑按钮：设置cookie
    edit_button.onclick=function(noteId){
		alert("编辑按钮开始");
        
       // setCookie("noteId",noteId,1);
		//alert(getCookie("noteId"));
	    window.location.href="/i/noteuser/noteUser/toeditLast";
	}
    //新建笔记按钮:删除cookie
    new_note_button.onclick=function(){
		alert("新建笔记按钮");
        //document.cookie = "note=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        window.location.href="/i/noteuser/noteUser/tonewnote";
    }
	//查看笔记列表
	
	
	
	//查看回收站列表
	
}

function setCookie(cname,cvalue,exdays){
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
 } 


function getCookie(cname){
 var name = cname + "=";
 var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name)==0) 
        return c.substring(name.length,c.length);
    }
    return "";
 } 

function search_button(){
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("POST","/i/note/note/search",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var search_data="title="+document.getElementById("search_title").value+"&userId="+userId.toString();
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200)
                {
                    var search_result=JSON.parse(xmlhttp.responseText);
                    if(search_result.result==1)
                    {
                        document.getElementById("note_title").innerHTML=search_result.title;
						document.getElementById("date").innerHTML=search_result.updateTime;
						document.getElementById("article").innerHTML=search_result.text;
						//设置cookie
						setCookie("noteId",search_result.noteId,1);
                    }
                    else{
                       alert("无此条笔记的搜索结果！");
                    }
                }
            }
        }
        xmlhttp.send(search_data);
    }

function logout(){
	window.location.href="/i/noteuser/noteUser/noteUser";
}
</script>
</body>
</html>



















